<?php
namespace app\admin\logic;
use app\common\model\Video;
use think\Db;
use app\common\logic\BaseLogic;
Class VideoLogic extends BaseLogic
{
    public function listing($page_size = 10)
    {
        $cate = Db::table('cate')->select();
        $cateId = [];
        foreach($cate as $vals){
            $cateId[$vals['id']] = $vals['name'];
        }

        $lists = Video::paginate($page_size)->toArray();
        if($lists['count'] != 0) {
            foreach($lists['data'] as $key=>$val) {
                $lists['data'][$key]['pname'] = empty($val['pid'])?'':$cateId[$val['pid']];

                switch ($val['type']) {
                    case 1:
                        $lists['data'][$key]['type'] = '热门节目';
                        break;
                    case 2:
                        $lists['data'][$key]['type'] = '高级私人订制';
                        break;
                    case 3:
                        $lists['data'][$key]['type'] = '高级私人订制';
                        break;
                }
            }
        }
        return $lists;
    }

    public function create($data)
    {
//        $data['create_time'] = time();
//        $data['update_time'] = time();
        $data['create_time'] = strtotime($data['create_time']);
        (new Video())->save($data);
        return true;
    }

    public function update($data, $methods = 'post')
    {
        if ($methods === 'get') {
            $r = Video::get($data);
        } else {
            if (empty($data['image']))
                unset($data['image']);
            if (empty($data['video']))
                unset($data['video']);
            $r = Video::update($data);
        }
        return $r;
    }

    public function delete($ids)
    {
        $str = trim($ids,',');
        $_arr = explode(',',$str);
        $where[] = ['id','in',$_arr];
        $r = Video::where($where)->delete();
        return $r;
    }
}
